#!/bin/sh -e
#
#    logo: determine which logo to use
#
#    Copyright (C) 2008 Canonical Ltd.
#    Copyright (C) 2011 Dustin Kirkland
#
#    Authors: Dustin Kirkland <kirkland@byobu.co>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, version 3 of the License.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

get_distro() {
	_RET=
	if [ -n "$DISTRO" ]; then
		# Allow manual override of distro in $BYOBU_CONFIG_DIR/status
		_RET="$DISTRO"
	elif [ -r "/etc/issue" ]; then
		# Otherwise, check /etc/issue
		_RET=`grep -m1 "^[A-Za-z]" /etc/issue`
	elif $BYOBU_TEST lsb_release >/dev/null 2>&1; then
		# This can be a very expensive operation, do it only
		# if all other methods fail
		_RET=`lsb_release -s -a 2>/dev/null`
	elif $BYOBU_TEST sw_vers >/dev/null 2>&1; then
		_RET=`sw_vers 2>/dev/null`
	elif $BYOBU_TEST uname >/dev/null 2>&1; then
		_RET=`uname -s`
	else
		# No idea!
		_RET="[B]"
	fi
}

__logo_detail() {
	MARKUP="false"
	__logo
}

__logo() {
	local logo=
	if [ -n "$LOGO" ]; then
		# Allow users to define their own logo in $BYOBU_CONFIG_DIR/status
		printf "%s" "$LOGO"
		return
	fi
	get_distro
	local distro="$(printf "%s" "$_RET" | $BYOBU_SED 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/')"
	case "$distro" in
		*ubuntu*)
			if $MARKUP; then
				if [ "$BYOBU_BACKEND" = "tmux" ] && [ "$BYOBU_CHARMAP" = "UTF-8" ]; then
					if [ ! "$VTE_CJK_WIDTH" = "1" ] && [ !  "$VTE_CJK_WIDTH" = "wide" ] ; then
						color m W; printf " u "; color -
					else
						color m W; printf " U "; color -
					fi
				else
					color m W; printf " U "; color -
				fi
			else
				printf " U "
			fi
		;;
		*altlinux*)
			logo="alt"
			$MARKUP && printf "$(color Y k)%s$(color -)" "$logo" || printf "$logo"
		;;
	 	*arch*)
			logo=" A "
			$MARKUP && printf "$(color b w b)%s$(color -)" "$logo" || printf "$logo"
		;;
		*centos*)
			logo="****"
			$MARKUP && printf "$(color g w)*$(color -)$(color m w)*$(color -)$(color y w)*$(color -)$(color b w)*$(color -)" || printf "$logo"
		;;
		*debian*)
			logo=" @ "
			$MARKUP && printf "$(color w r)%s$(color -)" "$logo" || printf "$logo"
		;;
		*fedora*)
			logo=" f "
			$MARKUP && printf "$(color b w)%s$(color -)" "$logo" || printf "$logo"
		;;
		*foresight*)
			logo="<@>"
			$MARKUP && printf "$(color b W g)%s$(color -)" "$logo" || printf "$logo"
		;;
		*gentoo*)
			logo=" > "
			$MARKUP && printf "$(color b c w)%s$(color -)" "$logo" || printf "$logo"
		;;
		*mac*|*darwin*)
			if [ "$BYOBU_BACKEND" = "tmux" ] && [ "$BYOBU_CHARMAP" = "UTF-8" ]; then
				# The Apple option character
				#logo=" ⌘  "
				# The Apple logo -- only visible from Mac terminals
				logo=""
			else
				logo=" X "
			fi
			$MARKUP && printf "$(color w k)%s$(color -)" "$logo" || printf "$logo"
		;;
		*mandriva*)
			logo=" (* "
			$MARKUP && printf "$(color b k c) ($(color -)$(color b k y)*$(color -) " || printf "$logo"
		;;
		*mint*)
			logo=" lm "
			$MARKUP && printf "$(color g w)$logo$(color -)$(color g w)$(color -) " || printf "$logo"
		;;
		*red*hat*)
			logo=" RH "
			$MARKUP && printf "$(color R k)%s$(color -)" "$logo" || printf "$logo"
		;;
		*rockhopper*)
			logo="palm"
			$MARKUP && printf "$(color k w)%s$(color -)" "$logo" || printf "$logo"
		;;
		*slackware*)
			logo=".S "
			$MARKUP && printf "$(color u B k)%s$(color -)" "$logo" || printf "$logo"
		;;
		*suse*)
			logo="SuSE"
			$MARKUP && printf "$(color W g)%s$(color -)" "$logo" || printf "$logo"
		;;
		*xandros*)
			logo=" X "
			$MARKUP && printf "$(color W r)%s$(color -)" "$logo" || printf "$logo"
		;;
		*)
			logo="[B]"
			$MARKUP && printf "$(color b k W)%s$(color -)" "$logo" || printf " $logo "
		;;
	esac
	printf " "
}

# vi: syntax=sh ts=4 noexpandtab
